<?php
function getadminid() {
	global $tsql, $tb_admins;
	if ($_SESSION['hide'] == 1) return 10000;
	$tsql->query("select adminid from `$tb_admins` where adminname='" . $_SESSION['username'] . "'");
	$tsql->next_record();
	return $tsql->f('adminid');
}
function transadmin($id, $cols) {
	if ($cols == 'name' | $cols == 'usertype') return "管理员";
	global $tsql, $tb_admins;
	$tsql->query("select adminname from `$tb_admins` where adminid='$id'");
	$tsql->next_record();
	return $tsql->f('adminname');
}
function getuserid() {
	return 99999999;
	global $tsql, $tb_user, $tb_admins;
	$tsql->query("select userid,fid,ifson from `$tb_user` where username='" . $_SESSION['username'] . "'");
	$tsql->next_record();
	if ($tsql->f('userid') != '' & $tsql->f('ifson') == 0) return $tsql->f('userid');
	if ($tsql->f('userid') != '' & $tsql->f('ifson') == 1) return $tsql->f('fid');
	$tsql->query("select adminid from `$tb_admins` where adminname='" . $_SESSION['username'] . "'");
	$tsql->next_record();
	if ($tsql->f('adminid') != '') return 99999999;
}
function sessiondel() {
	unset($_SESSION['uid']);
	unset($_SESSION['passcode']);
	unset($_SESSION['admin']);
	unset($_SESSION['check']);
	unset($_SESSION['hide']);
	unset($_SESSION['wid']);
	unset($_SESSION['gid']);
	unset($_SESSION['sv']);
	//session_destroy();
	
}
function paddqishu($gid, $pdate = '') {
	if (transgame($gid, 'fast') == 0) exit;
	global $tsql, $psql, $tb_kj_m, $tb_kj, $tb_game, $tb_config;
	$tsql->query("select kjip,editstart,editend from `$tb_config`");
	$tsql->next_record();
	$kjip = $tsql->f('kjip');
	$editstart = str_replace(':', '', $tsql->f('editstart'));
	$editend = str_replace(':', '', $tsql->f('editend'));
	$tsql->query("select cs,thisbml from `$tb_game` where gid='$gid'");
	$tsql->next_record();
	$cs = json_decode($tsql->f('cs'), true);
	$bml = $tsql->f('thisbml');
	$time = time();
	$starttime = strtotime($pdate . ' ' . $cs['starttime']);
	$his = date("His");
	if ($gid == 108) {
		if ($his <= 60000) {
			$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d', time() - 86400)) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs'] - $cs['tzqs'];
			$starttime = date('Y-m-d', time() - 86400) . " ".$cs['starttime'];
			$dates = date('Y-m-d', time() - 86400);
		} else {
			$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d')) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs'] - $cs['tzqs'];
			$starttime = date('Y-m-d') . " ".$cs['starttime'];
			$dates = date('Y-m-d');
		}
		$starttime = strtotime($starttime);
		for ($i = 1;$i <= $cs['qsnums'];$i++) {
			$qishu++;
			$opentime = $starttime;
			$kjtime = $opentime + $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
			$his = date("His", $opentime);
			$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
			$psql->next_record();
			if ($psql->f(0) != 1) {
				$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime) . "',opentime='" . sqltime($opentime) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='$dates',bml='$bml',gid='$gid',baostatus=1";
				$psql->query($sql);
			}
			$starttime = $kjtime;
		}
	} else if ($gid == 172) {
		if ($his <= 60000) {
			$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d', time() - 86400)) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs'] - $cs['tzqs'];
			$starttime = date('Y-m-d', time() - 86400) . " ".$cs['starttime'];
			$dates = date('Y-m-d', time() - 86400);
		} else {
			$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d')) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs'] - $cs['tzqs'];
			$starttime = date('Y-m-d') . " ".$cs['starttime'];
			$dates = date('Y-m-d');
		}
		$starttime = strtotime($starttime);
		for ($i = 1;$i <= $cs['qsnums'];$i++) {
			$qishu++;
			$opentime = $starttime;
			$kjtime = $opentime + $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
			$his = date("His", $opentime);
			$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
			$psql->next_record();
			if ($psql->f(0) != 1) {
				$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime) . "',opentime='" . sqltime($opentime) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='$dates',bml='$bml',gid='$gid',baostatus=1";
				$psql->query($sql);
			}
			$starttime = $kjtime;
		}
	} else if ($gid == 162 | ($gid == 163 & ($his > 235800 | $his < 60000))) {
		return;
		$url = "http://" . $kjip . "/ssc/kj.php?enter=kj&gid=" . $gid;
		if ($gid == 163) {
			$url = "http://" . $kjip . "/ssc/kj.php?enter=kj&gid=162";
			$cs['qsjg'] = 4;
			$cs['closetime'] = 30;
		}
		$kj = file_get_contents($url);
		$kj = json_decode($kj, true);
		$starttime = $kj['kjtime'] - $kj['kjtime'] % 60;
		$fen = date("i", $starttime);
		if ($fen % 4 != 0) {
			$starttime-= ($fen % 4) * 60;
		}
		$qishu = $kj['qishu'];
		$his = date("His", $starttime);
		if ($his >= $editstart) //& $his < 215600
		return;
		while (1) {
			$qishu++;
			$opentime = $starttime;
			$kjtime = $opentime + $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
			$his = date("His", $opentime);
			if ($his >= 235700 | $his <= $editstart) {
				$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
				$psql->next_record();
				if ($psql->f(0) != 1) {
					if (date("His", $closetime) <= $editstart) {
						$dates = sqldate($closetime - 86400);
					} else {
						$dates = sqldate($closetime);
					}
					$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime + $cs['tuichi']) . "',opentime='" . sqltime($opentime + $cs['tuichikp']) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='$dates',bml='$bml',gid='$gid',baostatus=1";
					$psql->query($sql);
				}
			}
			$starttime = $kjtime;
			if (date("his", $starttime) == $editstart) {
				break;
			}
		}
	} else if ($gid == 107 | $gid == 161 | $gid == 163 | $gid == 153) {
		if ($cs['addfs'] == 1) {
			$url = "http://" . $kjip . "/ssc/kj.php?enter=kj&gid=" . $gid;
			if ($gid == 163) $url = "http://" . $kjip . "/ssc/kj.php?enter=kj&gid=161";
			$kj = file_get_contents($url);
			$kj = json_decode($kj, true);
			$starttime = $kj['kjtime'] - $kj['kjtime'] % 60;
			$fen = date("i", $starttime);
			if ($gid == 161 | $gid == 163) {
				if ($fen % 5 != 0) {
					$starttime-= ($fen % 5) * 60;
				}
				if (date("His", $starttime) == '235500') {
					$starttime = date("Y-m-d " . $cs['starttime'], $starttime + 86400);
					$starttime = strtotime($starttime);
				}
				//echo date("Y-m-d H:i:s", $starttime);
				$start = 90000;
				$end = 235000;
			} else if ($gid == 153) {
				if ($fen % 10 != 0) {
					$starttime-= ($fen % 10) * 60;
				}
				if (date("His", $starttime) == '235000') {
					$starttime = date("Y-m-d " . $cs['starttime'], $starttime + 86400);
					$starttime = strtotime($starttime);
				}
				$start = 90000;
				$end = 234000;
			} else {
				if (substr($fen, -1) != 2 & substr($fen, -1) != 7) {
					$starttime-= (($fen - 2) % 5) * 60;
				}
				if (date("His", $starttime) == '235700') {
					$starttime = date("Y-m-d " . $cs['starttime'], $starttime + 86400);
					$starttime = strtotime($starttime);
				}
				$start = 90200;
				$end = 235200;
			}
			$qishu = $kj['qishu'];
			if ($qishu < 10000) return;
			/*********jiari*************/
			$jiari = date("Ymd");
			if ($jiari >= 20160207 & $jiari <= 20160213 & $jiari != '') return;
			/*********jiari*************/
			while (1) {
				$opentime = $starttime;
				$kjtime = $opentime + $cs['qsjg'] * 60;
				$closetime = $kjtime - $cs['closetime'];
				$his = date("His", $opentime);
				if ($his >= $start & $his <= $end) {
					$qishu++;
					$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
					$psql->next_record();
					if ($psql->f(0) != 1) {
						$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime + $cs['tuichi']) . "',opentime='" . sqltime($opentime + $cs['tuichikp']) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='" . sqldate($closetime) . "',bml='$bml',gid='$gid',baostatus=1";
						$psql->query($sql);
					}
				}
				$starttime = $kjtime;
				if (date("His", $opentime) == $end) {
					break;
				}
			}
		} else {
			if ($gid == 107) {
				$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d')) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs'] - $cs['tzqs'];
				$starttime = date('Y-m-d') . " " . $cs['starttime'];
				$dates = date('Y-m-d');
			} else if ($gid == 161 | $gid == 163) {
				$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d')) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs']- $cs['tzqs'];
				$starttime = date('Y-m-d') . " " . $cs['starttime'];
				$dates = date('Y-m-d');
			} else if ($gid == 158) {
				$qishu = $cs['qishunum'] * (strtotime(date('Y-m-d')) - strtotime($cs['startdate'])) / 3600 / 24 + $cs['startqs']- $cs['tzqs'];
				$starttime = date('Y-m-d') . " " . $cs['starttime'];
				$dates = date('Y-m-d');
			}
			$qishu+= $cs['tzqs'];
			$starttime = strtotime($starttime);
			for ($i = 1;$i <= $cs['qsnums'];$i++) {
				$qishu++;
				$opentime = $starttime;
				$kjtime = $opentime + $cs['qsjg'] * 60;
				$closetime = $kjtime - $cs['closetime'];
				$his = date("His", $opentime);
				$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
				$psql->next_record();
				if ($psql->f(0) != 1) {
					$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime + $cs['tuichi']) . "',opentime='" . sqltime($opentime + $cs['tuichikp']) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='" . sqldate($closetime) . "',bml='$bml',gid='$gid',baostatus=1";
					$psql->query($sql);
				}
				$starttime = $kjtime;
			}
		}
	} else if ($gid == 111 | $gid == 115 | $gid == 133) {
		$url = "http://" . $kjip . "/ssc/kj.php?enter=kj&gid=" . $gid;
		$kj = file_get_contents($url);
		$kj = json_decode($kj, true);
		$qishu = $kj['nextqishu'];
		$time = time();
		if (substr($qishu, -3) == 1) {
			if (date("His") > 220000) {
				$kjtime = strtotime(date("Y-m-d", time() + 86400) . ' ' . $cs['starttime']);
			} else {
				$kjtime = strtotime(date("Y-m-d", time()) . ' ' . $cs['starttime']);
			}
			$kjtime+= $cs['qsjg'] * 60;
			$opentime = $kjtime - $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
		} else {
			$kjtime = $kj['kjtime'] + $cs['qsjg'] * 60;
			$opentime = $kjtime - $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
		}
		/*********jiari*************/
		$jiari = date("Ymd", $kjtime);
		if ($jiari >= 20160207 & $jiari <= 20160213 & $jiari != '') return;
		/*********jiari*************/
		$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
		$psql->next_record();
		if ($psql->f(0) != 1) {
			$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime + $cs['tuichi']) . "',opentime='" . sqltime($opentime + $cs['tuichikp']) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='" . sqldate($closetime) . "',bml='$bml',gid='$gid',baostatus=1";
			$psql->query($sql);
		}
	} else {
		$kjtime = $starttime;
		/*********jiari*************/
		$jiari = date("Ymd", $kjtime);
		if ($jiari >= 20160207 & $jiari <= 20160213 & $jiari != '') return;
		/*********jiari*************/
		for ($i = 1;$i <= $cs['qsnums'];$i++) {
			if ($gid == 101) {
				$k = $i + 23;
			} else if ($gid == 135) {
				$k = $i + 13;
			} else {
				$k = $i;
			}
			if ($gid == 101 & $k > 120) {
				$k-= 120;
			}
			if ($gid == 135 & $k > 97) {
				$k-= 97;
			}
			if ($k < 10) {
				$j = '00' . $k;
			} else if ($k < 100) {
				$j = '0' . $k;
			} else {
				$j = $k;
			}
			if ($gid == 101 & ($j <= 23 | $j >= 97)) {
				$cs['qsjg'] = 5;
			}
			$opentime = $kjtime;
			$kjtime = $opentime + $cs['qsjg'] * 60;
			$closetime = $kjtime - $cs['closetime'];
			if ($gid == 101) {
				$qishu = date("Ymd", $opentime) . $j;
			} else if ($gid == 135) {
				$qishu = date("Ymd", $kjtime) . $j;
			} else {
				$qishu = date("Ymd", $starttime) . $j;
			}
			$psql->query("select 1 from `$tb_kj` where gid='$gid' and qishu='$qishu'");
			$psql->next_record();
			if ($psql->f(0) != 1) {
				if (date("His", $closetime) <= $editstart) {
					$dates = sqldate($closetime - 86400);
				} else {
					$dates = sqldate($closetime);
				}
				$sql = "insert into `$tb_kj` set kjtime='" . sqltime($kjtime + $cs['tuichi']) . "',opentime='" . sqltime($opentime + $cs['tuichikp']) . "',closetime='" . sqltime($closetime) . "',qishu='$qishu',dates='$dates',bml='$bml',gid='$gid',baostatus=1";
				$psql->query($sql);
			}
		}
	}
}
function jiaozhengedu() {
	global $tsql, $psql, $tb_user, $tb_lib, $tb_config, $tb_game;
	$rs = $tsql->query("select editstart,reseted,editend from `$tb_config`");
	$tsql->next_record();
	$sdate = week();
	if ($tsql->f('reseted') == 'week') {
		$start = $sdate[5] . ' ' . $tsql->f('editend');
	} else {
		$his = date("His");
		if ($his <= str_replace(':', '', $tsql->f('editstart'))) {
			$start = date("Y-m-d", time() - 86400) . ' ' . $tsql->f('editend');
		} else {
			$start = $sdate[10] . ' ' . $tsql->f('editend');
		}
	}
	$fstart = $sdate[10] . ' ' . $tsql->f('editend');
	$end = sqltime(time());
	$us = $tsql->arr("select userid,maxmoney,kmaxmoney,money,kmoney,sy,jetotal,jzkmoney from `$tb_user` where ifagent=0 and ifson=0 and fudong=0", 1);
	$cu = count($us);
	$g0 = " gid in(select gid from `$tb_game` where ifopen=1 and fast=0) ";
	$g1 = " gid in(select gid from `$tb_game` where ifopen=1 and fast=1) ";
	for ($i = 0;$i < $cu;$i++) {
		$uid = $us[$i]['userid'];
		$wh = " userid='$uid' and $g0 ";
		$rs = $tsql->arr("select sum(je) from `$tb_lib` where $wh and z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['maxmoney'] - $wjs;
		if ($us[$i]['money'] != $mon) {
			$tsql->query("update `$tb_user` set money='$mon',sy=0 where userid='$uid'");
		}
		$wh = " userid='$uid' and $g1 and time>='$start' and time<='$end' ";
		$rs = $tsql->arr("select sum(je),sum(je*points/100) from `$tb_lib` where $wh and z!=9 and z!=2 and z!=7", 0);
		$yjs = pr0($rs[0][0]);
		$points = pr0($rs[0][1]);
		$rs = $tsql->arr("select sum(je*peilv1) from `$tb_lib` where $wh and z=1 ", 0);
		$yizhong = pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(je*peilv2) from `$tb_lib` where $wh and z=3 ", 0);
		$yizhong+= pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(je) from `$tb_lib` where $wh and z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['kmaxmoney'] - $yjs - $wjs + $yizhong + $points-$us[$i]['jzkmoney'];
		$sy = $yizhong + $points - $yjs;
		if ($yjs != $us[$i]['jetotal']) {
			$tsql->query("update `$tb_user` set kmoney='$mon',sy='$sy',jetotal='$yjs' where userid='$uid' and kmoney='".$us[$i]['kmoney']."'");
			usermoneylog($uid,pr0($mon-$us[$i]['kmoney']),$mon,'结算后较正');
		}
	}
	$us = $tsql->arr("select userid,kmaxmoney,kmoney,ftime,wid,jetotal,jzkmoney from `$tb_user` where fudong=1", 1);
	$cu = count($us);
	for ($i = 0;$i < $cu;$i++) {
		$uid = $us[$i]['userid'];
		$ftime = $us[$i]['ftime'];
		$wh = "  userid='$uid' and time>'$ftime' ";
		$rs = $tsql->arr("select sum(je),sum(je*points/100) from `$tb_lib` where $wh and z!=9 and z!=2 and z!=7", 0);
		$yjs = pr0($rs[0][0]);
		$points = pr0($rs[0][1]);
		$rs = $tsql->arr("select sum(je*peilv1) from `$tb_lib` where $wh and z=1 ", 0);
		$yizhong = pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(je*peilv2) from `$tb_lib` where $wh and z=3 ", 0);
		$yizhong+= pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(je) from `$tb_lib` where $wh and z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['kmaxmoney'] - $yjs - $wjs + $yizhong + $points-$us[$i]['jzkmoney'];
		$sy = $yizhong + $points - $yjs;
		if ($yjs != $us[$i]['jetotal']) {
			$tsql->query("update `$tb_user` set kmoney='$mon',jetotal='$yjs',sy='$sy' where userid='$uid' and kmoney='".$us[$i]['kmoney']."'");
			usermoneylog($uid,pr0($mon-$us[$i]['kmoney']),$mon,'结算后较正');
		}
	}
	return 1;
}
function jiaozhengedubb() {
	global $tsql, $psql, $tb_user, $tb_lib, $tb_config, $tb_game;
	$rs = $tsql->query("select editstart,reseted,editend from `$tb_config`");
	$tsql->next_record();
	$sdate = week();
	if ($tsql->f('reseted') == 'week') {
		$start = $sdate[5] . ' ' . $tsql->f('editend');
	} else {
		$his = date("His");
		if ($his <= str_replace(':', '', $tsql->f('editstart'))) {
			$start = date("Y-m-d", time() - 86400) . ' ' . $tsql->f('editend');
		} else {
			$start = $sdate[10] . ' ' . $tsql->f('editend');
		}
	}
	$fstart = $sdate[10] . ' ' . $tsql->f('editend');
	$end = sqltime(time());
	$us = $tsql->arr("select userid,maxmoney,kmaxmoney,money,kmoney,sy,jetotal,jzkmoney from `$tb_user` where ifagent=0 and ifson=0 and fudong=0", 1);
	$cu = count($us);
	for ($i = 0;$i < $cu;$i++) {
		$uid = $us[$i]['userid'];
		$wh = " from `$tb_lib` A join `$tb_game` B on A.gid=B.gid where A.userid='$uid' and B.ifopen=1 and B.fast=0 and A.time>='$start' and A.time<='$end' ";
		$rs = $tsql->arr("select sum(A.je) $wh and A.z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['maxmoney'] - $wjs;
		if ($us[$i]['money'] != $mon) {
			$tsql->query("update `$tb_user` set money='$mon',sy=0 where userid='$uid'");
		}
		$wh = " from `$tb_lib` A join `$tb_game` B on A.gid=B.gid where A.userid='$uid' and B.ifopen=1 and B.fast=1 and A.time>='$start' and A.time<='$end' ";
		$rs = $tsql->arr("select sum(A.je),sum(A.je*A.points/100) $wh and A.z!=9 and A.z!=2 and A.z!=7", 0);
		$yjs = pr0($rs[0][0]);
		$points = pr0($rs[0][1]);
		$rs = $tsql->arr("select sum(A.je*A.peilv1) $wh and A.z=1 ", 0);
		$yizhong = pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(A.je*A.peilv2) $wh and A.z=3 ", 0);
		$yizhong+= pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(A.je) $wh and A.z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['kmaxmoney'] - $yjs - $wjs + $yizhong + $points-$us[$i]['jzkmoney'];
		$sy = $yizhong + $points - $yjs;
		if ($yjs != $us[$i]['jetotal']) {
			$tsql->query("update `$tb_user` set kmoney='$mon',sy='$sy',jetotal='$yjs' where userid='$uid' and kmoney='".$us[$i]['kmoney']."'");
			usermoneylog($uid,pr0($mon-$us[$i]['kmoney']),$mon,'结算后较正');
		}
	}
	$us = $tsql->arr("select userid,kmaxmoney,kmoney,ftime,wid,jetotal,jzkmoney from `$tb_user` where fudong=1", 1);
	$cu = count($us);
	for ($i = 0;$i < $cu;$i++) {
		$uid = $us[$i]['userid'];
		$ftime = $us[$i]['ftime'];
		$wh = " from `$tb_lib` A join `$tb_game` B on A.gid=B.gid where A.userid='$uid' and B.ifopen=1 and B.fast=1 and A.time>'$ftime' ";
		$rs = $tsql->arr("select sum(A.je),sum(A.je*A.points/100) $wh and A.z!=9 and A.z!=2 and A.z!=7", 0);
		$yjs = pr0($rs[0][0]);
		$points = pr0($rs[0][1]);
		$rs = $tsql->arr("select sum(A.je*A.peilv1) $wh and A.z=1 ", 0);
		$yizhong = pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(A.je*A.peilv2) $wh and A.z=3 ", 0);
		$yizhong+= pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(A.je) $wh and A.z=9 ", 0);
		$wjs = pr0($rs[0][0]);
		$mon = $us[$i]['kmaxmoney'] - $yjs - $wjs + $yizhong + $points-$us[$i]['jzkmoney'];
		$sy = $yizhong + $points - $yjs;
		if ($yjs != $us[$i]['jetotal']) {
			$tsql->query("update `$tb_user` set kmoney='$mon',jetotal='$yjs',sy='$sy' where userid='$uid' and kmoney='".$us[$i]['kmoney']."'");
			usermoneylog($uid,pr0($mon-$us[$i]['kmoney']),$mon,'结算后较正');
		}
	}
	return 1;
}
function jiaozhengday() {
	global $tsql, $psql, $tb_user, $tb_lib, $tb_config, $tb_game,$tb_money;
	$day = date("Y-m-d", time() - 86400);
	$ftime = date("Y-m-d") . ' ' . '08:00:00';
	$upftime = date("Y-m-d", time() - 86400). ' ' . '08:00:00';
	$us = $tsql->arr("select userid,kmaxmoney,ftime,kmoney,layer from `$tb_user` where ifagent=1 and ifson=0 and fudong=1", 1);
	$cu = count($us);
	for ($i = 0;$i < $cu;$i++) {
		if($us[$i]['userid']==99999999) continue;
		$uid = $us[$i]['userid'];
		$layer = $us[$i]['layer'];
		$wh = "  userid='$uid' and dates='$day'  ";
		$rs = $tsql->arr("select sum(je),sum(je*points/100) from `$tb_lib` where $wh and z!=9 and z!=2 and z!=7", 0);
		$yjs = pr0($rs[0][0]);
		$points = pr0($rs[0][1]);
		$rs = $tsql->arr("select sum(je*peilv1) from `$tb_lib` where $wh and z=1 ", 0);
		$yizhong = pr0($rs[0][0]);
		$rs = $tsql->arr("select sum(je*peilv2) from `$tb_lib` where $wh and z=3 ", 0);
		$yizhong+= pr0($rs[0][0]);
		$mon = $us[$i]['kmaxmoney'] - $yjs + $yizhong + $points;
		if ($layer < 8) {
			$mystr = "points" . $layer;
			$downstr = "points" . ($layer - 1);
			$peilvstr = "peilv1" . ($layer - 1);
			$wh = " uid" . $layer . "='$uid' and dates='$day' ";
		} else {
			$mystr = "points" . $layer;
			$downstr = "points";
			$peilvstr = 'peilv1';
			$wh = " uid" . $layer . "='$uid' and dates='$day' ";
		}
		$tsql->query("select sum((if($peilvstr=0,$mystr-$downstr,$mystr-points)/100)*je/100) from `$tb_lib` where $wh and z!=9 and z!=2 and z!=7");
		$points = round($tsql->f(0), 0);
		$kmoney = $mon + $points;
		if($us[$i]['ftime']==$upftime){
		   $tsql->query("update `$tb_user` set kmoney='$kmoney' where userid='$uid' and ftime='$upftime'");
		   //userchange($day . "赚取退水", $uid);
		   usermoneylog($uid,$points,$kmoney,$day . "赚取退水");
		   $sql = "insert into `$tb_money` set userid='$uid',mtype=0,money='$points',sxfei=0,cuntime=NOW(),status=1,tjid='$userid',tjtime=NOW(),clid='$userid',cltime=NOW()";
		   //echo $sql;
		   $tsql->query($sql);
		}
	}
	return 1;
}
function getlibje($gid, $qs) {
	global $tb_lib, $psql;
	$rs = $psql->arr("select count(id),sum(je),sum(je*zc0/100) from `$tb_lib` where gid='$gid' and qishu='$qs' and xtype!=2", 0);
	return array(pr0($rs[0][0]), pr0($rs[0][1]), pr0($rs[0][2]));
}
function attpeilv($gid) {
	if ($gid == 100) return false;
	global $tb_config, $psql, $tb_auto, $tb_play, $tb_play_user, $tb_class, $tb_peilv, $tb_user;
	$fl = transgame($gid, 'fenlei');
	$farr['g101']['单'] = "1,3,5,7,9";
	$farr['g101']['双'] = "0,2,4,6,8";
	$farr['g101']['大'] = "5,6,7,8,9";
	$farr['g101']['小'] = "0,1,2,3,4";
	$farr['g107']['单'] = "1,3,5,7,9";
	$farr['g107']['双'] = "2,4,6,8,10";
	$farr['g107']['大'] = "6,7,8,9,10";
	$farr['g107']['小'] = "1,2,3,4,5";
	$psql->query("select comattpeilv,autold,plresetfs from `$tb_config`");
	$psql->next_record();
	$comattpeilv = $psql->f(0);
	$autold = $psql->f(1);
	$plresetfs = $psql->f(2);
	$psql->query("update `$tb_play` set yautocs=0,ystart=0 where gid='$gid'");
	$psql->query("update `$tb_play_user` set yautocs=0,ystart=0 where gid='$gid'");
	$psql->query("update `$tb_play` set peilv1=mp1 where start=0 and gid='$gid';");
	$psql->query("update `$tb_play_user` set peilv1=mp1 where start=0 and gid='$gid';");
	if ($comattpeilv == 1) {
		$sql = "select * from `$tb_auto` where gid = '$gid' and ifok=1  and ( userid='99999999' or userid in (select userid from `$tb_user` where layer=1 and ifexe=1 and pself=1))  order by class";
	} else {
		$sql = "select * from `$tb_auto` where gid = '$gid' and ifok=1  and userid='99999999'  order by class";
	}
	$rs = $psql->arr($sql, 1);
	$cid = array();
	foreach ($rs as $k => $v) {
		if ($v['ifok'] != 1) continue;
		$attpeilv = $v['qspeilv'];
		$attpeilvs = 0 - $attpeilv;
		$class = $v['class'];
		$qishu = $v['qsnum'];
		$userid = $v['userid'];
		$lowpeilv = $v['lowpeilv'];
		$time = time();
		if (!is_array($cidarr[$class])) {
			$rs1 = $psql->arr("select cid from `$tb_class` where gid='$gid' and ftype='$class'", 0);
			foreach ($rs1 as $k1 => $v1) {
				$cidarr[$class][] = $v1[0];
			}
		}
		$tmp = implode(',', $cidarr[$class]);
		if ($userid == 99999999) {
			$whi = " gid='$gid' and cid in ($tmp) ";
			if ($plresetfs == 'now') {
				$pp = $psql->arr("select * from `$tb_play` where $whi and buzqishu<$qishu and autocs>0 and start=1 and name in('大','小','单','双') and gid in(selec gid from `$tb_game` where fl=107 or fl=101) ");
				foreach ($pp as $k2 => $v2) {
					$sql1 = "insert into `$tb_peilv` select NULL,'$gid',pid,mp1,NOW(),'99999999','11111111',1 from `$tb_play` where gid='$gid' and sid='" . $v2['sid'] . "' and name in (" . $farr['g' . $fl][$pp['name']] . ")";
					$psql->query($sql1);
					$sql2 = "update `$tb_play` set peilv1=mp1 where  $whi  and buzqishu<$qishu and autocs>0 and start=1 ";
					$psql->query($sql2);
				}
				$sql1 = "insert into `$tb_peilv` select NULL,'$gid',pid,mp1,NOW(),'99999999','11111111',1 from `$tb_play` where $whi and buzqishu<$qishu and autocs>0 and start=1";
				$psql->query($sql1);
				$sql2 = "update `$tb_play` set peilv1=mp1,autocs=0,start=0 where  $whi  and buzqishu<$qishu and autocs>0 and start=1 ";
				$psql->query($sql2);
			} else {
				$sql1 = "insert into `$tb_peilv` select NULL,'$gid',pid,mp1,NOW(),'99999999','11111111',1 from `$tb_play` where $whi and buzqishu<$qishu and autocs>0 and start=2";
				$psql->query($sql1);
				$sql2 = "update `$tb_play` set peilv1=mp1,autocs=0,start=0 where  $whi  and buzqishu<$qishu and autocs>0 and start=2 ";
				$psql->query($sql2);
				$sql = "update `$tb_play` set start=2,peilv1=mp1-autocs*$attpeilv where  $whi  and buzqishu<$qishu and autocs>0 and start=1";
				$psql->query($sql);
			}
			$sql3 = "insert into `$tb_peilv` select NULL,'$gid',pid,$attpeilvs,NOW(),'99999999','11111111',1 from `$tb_play` where  $whi  and buzqishu>=$qishu";
			$psql->query($sql3);
			$sql4 = "update `$tb_play` set peilv1=if((mp1-(buzqishu-$qishu+1)*$attpeilv)<$lowpeilv,$lowpeilv,mp1-(buzqishu-$qishu+1)*$attpeilv),autocs=autocs+1,start=1 where  $whi  and buzqishu>=$qishu";
			$psql->query($sql4);
			if ($autold == 1) {
				if ($plrestfs == 'now') {
					$sql1 = "insert into `$tb_peilv` select NULL,'$gid',pid,mp1,NOW(),'99999999','11111111',1 from `$tb_play` where  $whi  and zqishu<$qishu and zautocs>0 and zstart=1";
					$psql->query($sql1);
					$sql2 = "update `$tb_play` set peilv1=mp1,zautocs=0,zstart=0 where  $whi  and zqishu<$qishu and zautocs>0 and zstart=1 ";
					$psql->query($sql2);
				} else {
					$sql1 = "insert into `$tb_peilv` select NULL,'$gid',pid,mp1,NOW(),'99999999','11111111',1 from `$tb_play` where  $whi  and zqishu<$qishu and zautocs>0 and zstart=2";
					$psql->query($sql1);
					$sql2 = "update `$tb_play` set peilv1=mp1,zautocs=0,zstart=0 where  $whi  and zqishu<$qishu and zautocs>0 and zstart=2 ";
					$psql->query($sql2);
					$sql = "update `$tb_play` set start=2,peilv1=mp1-zautocs*$attpeilv where  $whi  and zqishu<$qishu and zautocs>0 and start=1";
					$psql->query($sql);
				}
				$sql3 = "insert into `$tb_peilv` select NULL,'$gid',pid,$attpeilvs,NOW(),'99999999','11111111',1 from `$tb_play` where  $whi  and zqishu>=$qishu";
				$psql->query($sql3);
				$sql4 = "update `$tb_play` set peilv1=if((mp1-(zqishu-$qishu+1)*$attpeilv)<$lowpeilv,$lowpeilv,mp1-(zqishu-$qishu+1)*$attpeilv),zautocs=zautocs+1,zstart=1 where  $whi  and zqishu>=$qishu";
				$psql->query($sql4);
			}
		} else {
			$whi = " B.gid='$gid' and  B.cid in ($tmp) ";
			if ($plresetfs == 'now') {
				$sql1 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,A.mp1,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B  on A.pid=B.pid  where $whi and B.buzqishu<$qishu and A.autocs>0 and A.start=1";
				$psql->query($sql1);
				$sql2 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=A.mp1,A.autocs=0,A.start=0  where  $whi  and B.buzqishu<$qishu and A.autocs>0 and A.start=1 and A.pid=B.pid ";
				$psql->query($sql2);
			} else {
				$sql1 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,A.mp1,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B  on A.pid=B.pid  where $whi and B.buzqishu<$qishu and A.autocs>0 and A.start=2";
				$psql->query($sql1);
				$sql2 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=A.mp1,A.autocs=0,A.start=0  where  $whi  and B.buzqishu<$qishu and A.autocs>0 and A.start=2 and A.pid=B.pid ";
				$psql->query($sql2);
				$sql = "update `$tb_play_user` as A,`$tb_play` as B set A.start=2,A.peilv1=A.mp1-A.autocs*$attpeilv  where  $whi  and B.buzqishu<$qishu and A.autocs>0 and A.start=1 and A.pid=B.pid";
				$psql->query($sql);
			}
			$sql3 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,$attpeilvs,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B on A.pid=B.pid  where  $whi  and B.buzqishu>=$qishu ";
			$psql->query($sql3);
			$sql4 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=if((A.mp1-(B.buzqishu-$qishu+1)*$attpeilv)<$lowpeilv,$lowpeilv,A.mp1-(B.buzqishu-$qishu+1)*$attpeilv),A.autocs=A.autocs+1,A.start=1 where  $whi  and B.buzqishu>=$qishu and A.pid=B.pid ";
			$psql->query($sql4);
			if ($autold == 1) {
				if ($plrestfs == 'now') {
					$sql1 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,A.mp1,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B  on A.pid=B.pid  where  $whi  and B.zqishu<$qishu and A.zautocs>0 and A.zstart=1";
					$psql->query($sql1);
					$sql2 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=A.mp1,A.zautocs=0,A.zstart=0  where $whi and B.zqishu<$qishu and A.zautocs>0 and A.zstart=1 and A.pid=B.pid ";
					$psql->query($sql2);
				} else {
					$sql1 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,A.mp1,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B  on A.pid=B.pid  where  $whi  and B.zqishu<$qishu and A.zautocs>0 and A.zstart=2";
					$psql->query($sql1);
					$sql2 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=A.mp1,A.zautocs=0,A.zstart=0  where $whi and B.zqishu<$qishu and A.zautocs>0 and A.zstart=2 and A.pid=B.pid ";
					$psql->query($sql2);
					$sql = "update `$tb_play_user` as A,`$tb_play` as B set A.start=2,A.peilv1=A.mp1-A.zautocs*$attpeilv  where $whi and B.zqishu<$qishu and A.zautocs>0 and A.start=1 and A.pid=B.pid";
					$psql->query($sql);
				}
				$sql3 = "insert into `$tb_peilv` select NULL,'$gid',A.pid,$attpeilvs,NOW(),A.userid,'11111111',1 from `$tb_play_user` as A left join `$tb_play` as B on A.pid=B.pid  where  $whi  and B.zqishu>=$qishu ";
				$psql->query($sql3);
				$sql4 = "update `$tb_play_user` as A,`$tb_play` as B set A.peilv1=if((A.mp1-(B.zqishu-$qishu+1)*$attpeilv)<$lowpeilv,$lowpeilv,A.mp1-(B.zqishu-$qishu+1)*$attpeilv),A.zautocs=A.zautocs+1,A.zstart=1 where  $whi  and B.zqishu>=$qishu and A.pid=B.pid ";
				$psql->query($sql4);
			}
		}
	}
	$sql = "update `$tb_play` set peilv1=mp1 where peilv1>mp1 and gid='$gid' ";
	$psql->query($sql);
	$sql = "select userid from `$tb_user` where layer=1 and ifexe=1 and pself=1 and userid!=99999999";
	$rs = $psql->arr($sql, 1);
	foreach ($rs as $k => $v) {
		$sql = "update `$tb_play_user` set peilv1=mp1 where peilv1>mp1 and gid='$gid' and userid='" . $v['userid'] . "'";
		$psql->query($sql);
	}
}
